package com.zzh.custom;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-09-29
 * @desc
 */
public class DynamicSplitView extends ViewGroup {

    protected int mCount = 1;
    protected int mOldCount = -1;

    public DynamicSplitView(Context context) {
        this(context, null);
    }

    public DynamicSplitView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public DynamicSplitView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    protected void init(Context context, AttributeSet attrs) {

    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        if (getWidth() == 0 || floats.size() != count) {
            return;
        }
        for (int i = 0; i < count; i++) {
            float left = getPaddingLeft();
            for (int i1 = 0; i1 < i; i1++) {
                left += floats.get(i1);
            }
            View view = getChildAt(i);
            float childWidth = floats.get(i);
            view.layout((int) left, getPaddingTop(), (int) (left + childWidth), b - getTop() - getPaddingBottom());
            if (view instanceof ViewGroup) {
                ViewGroup childView = (ViewGroup) view;
                for (int i1 = 0; i1 < childView.getChildCount(); i1++) {
                    if (mCount > i) {
                        childView.getChildAt(i1).getLayoutParams().width = i >= mOldCount ? (int) mSingleWidth + 10 : (int) childWidth + 10;
                    }
                }
            }
        }
    }

    private void print(Object... o){
        StringBuffer stringBuffer = new StringBuffer();
        for (Object o1 : o) {
            stringBuffer.append(o1).append('\n');
        }
        Log.i("zhangzhihao", ">print>" + stringBuffer.toString());
    }

    private List<Float> floats = new ArrayList<>();
    private ValueAnimator valueAnimator;
    private float mSingleWidth;
    private boolean isAnimatorStatus = false;


    public void setShowCount(int count, final ValueAnimator.AnimatorUpdateListener animatorUpdateListener) {
        if (mCount == count || count > getChildCount()) {
            return;
        }
        if (valueAnimator != null && valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
        mOldCount = mCount;
        mCount = count;
        final List<Float> list = new ArrayList<>();
        for (Float aFloat : floats) {
            list.add(aFloat);
        }
        valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(300);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                floats.clear();
                for (int i = 0; i < list.size(); i++) {
                    float nowWidth = list.get(i);
                    if (mCount <= i) {
                        // 缩小
                        if (nowWidth <= 0) {
                            floats.add(0F);
                        } else {
                            floats.add(nowWidth * (1 - value));
                        }
                    } else {
                        // 放大
                        floats.add(nowWidth + (mSingleWidth - nowWidth) * value);
                    }
                }
                isAnimatorStatus = value != 1;
                requestLayout();
                if (animatorUpdateListener != null) {
                    animatorUpdateListener.onAnimationUpdate(animation);
                }
            }
        });
        valueAnimator.start();
    }

    public void onClearAnimator() {
        if (valueAnimator != null && valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        if (width == 0) {
            return;
        }
        int count = getChildCount();
        if (count == 0) {
            return;
        }
        if (mCount <= 0) {
            return;
        }
        mSingleWidth = (width - getPaddingLeft() - getPaddingRight()) / (float) mCount;
        if (floats.size() > 0) {
            measureChildren(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        for (int i = 0; i < count; i++) {
            if (i < mCount) {
                floats.add(mSingleWidth);
            } else {
                floats.add(0F);
            }
        }
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }
}
